package top.llove.code.system.system.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.weaver.loadtime.Aj;
import org.csource.common.MyException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.llove.code.system.system.pojo.SysFile;
import top.llove.code.system.system.service.YSysConfigService;
import top.llove.code.system.system.service.YSysFileService;
import top.llove.common.utils.file.fastdfs.FastDFSUtils;
import top.llove.framework.aspectj.lang.annotation.Log;
import top.llove.framework.aspectj.lang.enums.BusinessType;
import top.llove.framework.web.controller.BaseController;
import top.llove.framework.web.domain.AjaxResult;
import top.llove.common.utils.poi.ExcelUtil;
import top.llove.framework.web.page.TableDataInfo;

/**
 * 文件上传Controller
 *
 * @author: 幸会
 * @date: 2021-12-19
 * @email: 1846724873@qq.com
 * @description: 文件上传Controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/system/file")
public class SysFileController extends BaseController
{
    @Autowired
    private YSysFileService sysFileService;

    @Autowired
    private YSysConfigService configService;

    /**
     * 查询文件上传列表
     */
    @PreAuthorize("@perm.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFile sysFile)
    {
        startPage();
        List<SysFile> list = sysFileService.selectSysFileList(sysFile);
        return getDataTable(list);
    }

    @GetMapping("/size")
    public AjaxResult getFileSize() {
        String size = configService.selectConfigByKey("sys.file.upload.size");
        DataSize parse = DataSize.parse(size);
        long sizeLong = parse.toBytes();
        return AjaxResult.success(sizeLong);
    }

    @PreAuthorize("@perm.hasPermi('system:file:upload')")
    @Log(title = "文件上传", businessType = BusinessType.UPLOAD)
    @PostMapping("/upload")
    public AjaxResult upload (MultipartFile file) throws MyException, IOException {
        Map<String, Object> upload = sysFileService.upload(file);
        return AjaxResult.success(upload);
    }

    /**
     * 文件下载
     * @author 幸会
     * @date 2021/12/26 19:25
     */
    @PreAuthorize("@perm.hasPermi('system:file.download')")
    @Log(title = "文件下载", businessType = BusinessType.DOWNLOAD)
    @GetMapping("/download/{id}")
    public void download (@PathVariable(name = "id") Long id, HttpServletResponse resp) throws IOException {
        sysFileService.download(id, resp);
    }



    /**
     * 获取文件上传详细信息
     */
    @PreAuthorize("@perm.hasPermi('system:file:query')")
    @GetMapping(value = "/{fileId}")
    public AjaxResult getInfo(@PathVariable("fileId") Long fileId)
    {
        return AjaxResult.success(sysFileService.selectSysFileByFileId(fileId));
    }

    /**
     * 新增文件上传
     */
    @PreAuthorize("@perm.hasPermi('system:file:add')")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFile sysFile)
    {
        return toAjax(sysFileService.insertSysFile(sysFile));
    }

    /**
     * 修改文件上传
     */
    @PreAuthorize("@perm.hasPermi('system:file:edit')")
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFile sysFile)
    {
        return toAjax(sysFileService.updateSysFile(sysFile));
    }

    /**
     * 删除文件上传
     */
    @PreAuthorize("@perm.hasPermi('system:file:remove')")
    @Log(title = "文件上传", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
    public AjaxResult remove(@PathVariable Long[] fileIds) throws Exception {
        return toAjax(sysFileService.deleteSysFileByFileIds(fileIds));
    }
}
